package com.mmall.controller.backend;


import com.google.common.collect.Maps;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Product;
import com.mmall.pojo.User;
import com.mmall.service.IFileService;
import com.mmall.service.IProductService;
import com.mmall.service.IUserSService;
import com.mmall.utils.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/manager/product/")
public class ProductManagerController {

    @Autowired
    private IUserSService iUserSService;
    @Autowired
    private IProductService iProductService;
    @Autowired
    private IFileService iFileService;

    @RequestMapping(value = "save.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse productSave(HttpSession session, Product product){

        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆,请登陆");
        }
        //校验一下是不是管理员
        if (iUserSService.checkAdminRole(user).isSuccess()){
            return iProductService.saveOrUpdateProduct(product);
        }else {
            return ServerResponse.createByErrorMessage("非管理员,无权限操作");
        }
    }


    @RequestMapping(value = "set_sale_status.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse setSaleStatus(HttpSession session, Integer productId,Integer status){

        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆,请登陆");
        }
        //校验一下是不是管理员
        if (iUserSService.checkAdminRole(user).isSuccess()){
            return iProductService.setSaleStatus(productId,status);
        }else {
            return ServerResponse.createByErrorMessage("非管理员,无权限操作");
        }
    }

    @RequestMapping(value = "detail.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getProductDetail(HttpSession session, Integer productId){

        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆,请登陆");
        }
        //校验一下是不是管理员
        if (iUserSService.checkAdminRole(user).isSuccess()){
            return iProductService.managerProductDetail(productId);
        }else {
            return ServerResponse.createByErrorMessage("非管理员,无权限操作");
        }
    }

    @RequestMapping("product_page_list.do")
    @ResponseBody
    public  ServerResponse getProductByPage(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1")
            Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){

        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆,请登陆");
        }
        //校验一下是不是管理员
        if (iUserSService.checkAdminRole(user).isSuccess()){

            return iProductService.getProductByPage(pageNum,pageSize);
        }else {
            return ServerResponse.createByErrorMessage("非管理员,无权限操作");
        }

    }

    @RequestMapping("product_search.do")
    @ResponseBody
    public  ServerResponse getProductSearchByPage(HttpSession session,String productName,Integer productId,
                                                  @RequestParam(value = "pageNum",defaultValue = "1")
            Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){

        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆,请登陆");
        }
        //校验一下是不是管理员
        if (iUserSService.checkAdminRole(user).isSuccess()){

            return iProductService.SearchProducts(productName,productId,pageNum,pageSize);
        }else {
            return ServerResponse.createByErrorMessage("非管理员,无权限操作");
        }

    }

    @RequestMapping("upload.do")
    @ResponseBody
    public ServerResponse uploadProductPic(@RequestParam(value = "upload_file",required = false) MultipartFile file,
                                           HttpServletRequest request){

        String path=request.getSession().getServletContext().getRealPath("upload");
        String targetfileName=iFileService.upload(file,path);
        String url= PropertiesUtil.getProperty("ftp.server.http.prefix")+targetfileName;
        Map<String,String> fileMap= Maps.newHashMap();
        fileMap.put("url",url);
        fileMap.put("uri",targetfileName);
        return ServerResponse.createBySuccess(fileMap);
    }
}
